package com.hunan.api.catv.service.biz.toupiao;

import com.google.gson.JsonObject;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.mq.MqBiz;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.Hmacsha256Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@Service
public class TouPiaoBiz extends ResultService {

    @Autowired
    CacheBiz cacheBiz;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    TouPiaoBiz touPiaoBiz;

    @Autowired
    MqBiz mqBiz;

    public ServiceResult addTicketToUser(Integer memberId, Integer type) {
        JsonObject msg = new JsonObject();
        msg.addProperty("memberId", memberId);
        msg.addProperty("type", type);
        //todo:蜗唱加票
		/*ServiceResult result = mqBiz.push(MqConstants.toupiao_exchange,MqConstants.toupiao_routingkey, GsonUtils.toJson(msg));
		if(!result.getStatus()) {
			return buildFail(false,"");
		}*/
        return buildOK(true, "");
    }

    public void addLottery(Integer memberId, Integer type, String activity) {
        JsonObject conf = GsonUtils.fromJson(cacheBiz.getSystemDict(activity), JsonObject.class);
        if (ObjectUtils.isEmpty(conf)) {
            log.warn(activity + " 未配置");
            return;
        }
        String domain = conf.get("domain").getAsString();
        String secret = conf.get("secret").getAsString();
        String activityId = "10026";
        String fromSource = conf.get("fromSource").getAsString();
        Map<String, Object> data = new TreeMap<String, Object>();
        Map<String, Object> paraMap = new HashMap<String, Object>();
        data.put("activityId", activityId);
        data.put("fromSource", fromSource);
        data.put("type", type.toString());
        data.put("userId", memberId.toString());
        paraMap.put("data", data);
        String sign = Hmacsha256Utils.toupiaoSign(GsonUtils.toJson(data), secret);
        paraMap.put("sign", sign);
        StringBuffer url = new StringBuffer(domain);
        url.append("/MemberInterface/addTicketsToUser");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
        try {
            HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(paraMap), headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
            log.info("[addTicketsToUser: {} {}]", response.getStatusCodeValue(), response.getBody());
        } catch (Exception e) {
            log.error("addTicketsToUser:{}", e);
        }
    }

    public void addLottery(Integer memberId, Integer type, String activity, String activityId) {
        JsonObject conf = GsonUtils.fromJson(cacheBiz.getSystemDict(activity), JsonObject.class);
        if (ObjectUtils.isEmpty(conf)) {
            log.warn(activity + " 未配置");
            return;
        }
        String domain = conf.get("domain").getAsString();
        String secret = conf.get("secret").getAsString();
        String fromSource = conf.get("fromSource").getAsString();
        Map<String, Object> data = new TreeMap<String, Object>();
        Map<String, Object> paraMap = new HashMap<String, Object>();
        data.put("activityId", activityId);
        data.put("fromSource", fromSource);
        data.put("type", type.toString());
        data.put("userId", memberId.toString());
        paraMap.put("data", data);
        String sign = Hmacsha256Utils.toupiaoSign(GsonUtils.toJson(data), secret);
        paraMap.put("sign", sign);
        StringBuffer url = new StringBuffer(domain);
        url.append("/MemberInterface/addLotteryNumToUser");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
        try {
            HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(paraMap), headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
            log.info("[addLotteryToUser: {} {}]", response.getStatusCodeValue(), response.getBody());
        } catch (Exception e) {
            log.error("addLotteryToUser:{}", e);
        }
    }

    public void addTicket(Integer memberId, Integer type, String activity) {
        JsonObject conf = GsonUtils.fromJson(cacheBiz.getSystemDict(activity), JsonObject.class);
        if (ObjectUtils.isEmpty(conf)) {
            log.warn(activity + " 未配置");
            return;
        }
        String domain = conf.get("domain").getAsString();
        String secret = conf.get("secret").getAsString();
        String activityId = conf.get("activityId").getAsString();
        String fromSource = conf.get("fromSource").getAsString();
        Map<String, Object> data = new TreeMap<String, Object>();
        Map<String, Object> paraMap = new HashMap<String, Object>();
        data.put("activityId", activityId);
        data.put("fromSource", fromSource);
        data.put("type", type.toString());
        data.put("userId", memberId.toString());
        paraMap.put("data", data);
        String sign = Hmacsha256Utils.toupiaoSign(GsonUtils.toJson(data), secret);
        paraMap.put("sign", sign);
        StringBuffer url = new StringBuffer(domain);
        url.append("/MemberInterface/addTicketsToUser");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
        try {
            HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(paraMap), headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
            log.info("[addTicketsToUser: {} {}]", response.getStatusCodeValue(), response.getBody());
        } catch (Exception e) {
            log.error("addTicketsToUser:{}", e);
        }
    }
}
